<template>
  <div id="app">
    <!-- 这是home页组件 -->
    <el-container class="outter-container">
      <!-- 头部区域 -->
      <el-header>
        <div>
          <!-- logo -->
          <img src="../assets/heima.png" alt="">
          <!-- 顶部标题 -->
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout"> 退出 </el-button>
      </el-header>
      <!-- 页面主体区域 -->
      <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isFold ? 64 + 'px' : 200 + 'px'">
        <div class="toggle-button" @click="toggleFold">|||</div>
        <!-- 侧边栏菜单 -->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" :unique-opened="true" :collapse="isFold" :collapse-transition="false" router :default-active="activePatho">
            <!-- 一级菜单 -->
          <el-submenu :index="item1.id + ''" :key="item1.id" v-for="item1 in menuList">
              <!-- 一级菜单的模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="iconsObj[item1.id]"></i>
                <!-- 文本 -->
                <span>{{ item1.authName }}</span>
              </template>
              <!-- 二级子菜单 -->
              <el-menu-item :index="'/' + item2.path" :key= "item2.id" v-for="item2 in item1.children" @click="saveActiceState('/' + item2.path)">
                <!-- 二级菜单模板 -->
                <template slot="title">
                  <!-- 图标 -->
                  <i class="el-icon-menu"></i>
                  <!-- 文本 -->
                  <span>{{ item2.authName }}</span>
                </template>
              </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data: function() {
    return {
      // 左侧菜单数据
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-user',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 左侧菜单栏是否折叠
      isFold: false,
      // 被激活的链接地址
      activePatho: ''
    }
  },
  // ******在created阶段请求左侧菜单数据
  created() {
    this.getMenuList()
    // ******在created阶段获取，之前在点击二级菜单时，存入sessionStorage中的，被激活的链接的路径，并赋值给 default-active
    this.activePatho = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 点击退出按钮后的操作
    logout() {
      // 清空sessionSrorage里的token
      window.sessionStorage.clear()
      // 跳转回登录页面
      this.$router.push('/login')
    },
    // 发送请求获取左侧菜单数据
    async getMenuList() {
      /* const result = await this.$xxx.get('menus')
      console.log(result) */
      // 使用async await简化promise对象
      const { data: res } = await this.$xxx.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取菜单列表数据失败：' + res.meta.msg)
      this.menuList = res.data
    },
    // 点击按钮，切换左侧菜单的折叠与展开
    toggleFold() {
      this.isFold = !this.isFold
    },
    // 保存左侧菜单栏二级菜单链接的激活状态
    saveActiceState(theOne) {
      // 点击二级菜单中的链接时，把对应的路径存入sessionStorage中
      window.sessionStorage.setItem('activePath', theOne)
      // 再次点击二级菜单时，除了存路径，还要给 default-active 更换值
      this.activePatho = window.sessionStorage.getItem('activePath')
    }
  }
}
</script>

<style lang="less" scoped>
  .outter-container {
    height: 100%;
  }
  .el-header{
    display: flex;
    justify-content: space-between;
    padding-left: 10px;
    align-items: center;
    color: #fff;
    background-color:#373D41;
    font-size: 20px;
      div {
        display: flex;
        align-items: center;
          img {
            margin-right: 16px;
          }
      }
      .el-button {
        font-size: 13px;
      }
  }
  .el-aside{
    background-color:#333744;
    font-size: 16px;
    .el-menu {
      border-right: 0;
    }
    .toggle-button {
      background-color: #4a5064;
      cursor: pointer;
      line-height: 24px;
      font-size: 10px;
      text-align: center;
      letter-spacing: 0.2em;
      color: #fff;
    }
  }
  .el-main{
    background-color:#eaedf1;
  }

  .iconfont {
    margin-right: 10px;
  }
</style>
